package com.yds.activityalias

import android.content.ComponentName
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatButton
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class AliasActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        findViewById<AppCompatButton>(R.id.mBtnNew).setOnClickListener {
            updateIconAndLabel("com.yds.activityalias.AliasSpringActivity")
        }

        findViewById<AppCompatButton>(R.id.mBtnOld).setOnClickListener {
            updateIconAndLabel("com.yds.activityalias.AliasActivity")
        }
    }

    private fun enableComponent(componentName: String) {
        val pm = applicationContext.packageManager
        pm.setComponentEnabledSetting(ComponentName(applicationContext.packageName, componentName), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP)
    }

    private fun disableComponent(componentName: String) {
        val pm = applicationContext.packageManager
        pm.setComponentEnabledSetting(ComponentName(applicationContext.packageName, componentName), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP)
    }

    private fun updateIconAndLabel(aliasToEnable: String) {
        // 禁用所有别名
        disableComponent("com.yds.activityalias.AliasActivity")
        disableComponent("com.yds.activityalias.AliasSpringActivity")
        // 启用目标别名
        enableComponent(aliasToEnable)
    }
}